﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Diagnostics;
using KC.Business;
using KC.DataAccess.Info;

namespace KC.Presentation.Info.InfoAggregator
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}
		private InfoDataContext dc;
		private System.Timers.Timer tSystem;
		public CollectionView GoFuckYourself;

		#region GlassExtension

		protected override void OnSourceInitialized(EventArgs e)
		{
			base.OnSourceInitialized(e);
			// This can't be done any earlier than the SourceInitialized event:
			GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
		}
		struct MARGINS
		{
			public MARGINS(Thickness t)
			{
				Left = (int)t.Left;
				Right = (int)t.Right;
				Top = (int)t.Top;
				Bottom = (int)t.Bottom;
			}
			public int Left;
			public int Right;
			public int Top;
			public int Bottom;
		}

		[DllImport("dwmapi.dll", PreserveSig = false)]
		static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

		[DllImport("dwmapi.dll", PreserveSig = false)]
		static extern bool DwmIsCompositionEnabled();

		public class GlassHelper
		{
			public static bool ExtendGlassFrame(Window window, Thickness margin)
			{
				if (!DwmIsCompositionEnabled())
					return false;

				IntPtr hwnd = new WindowInteropHelper(window).Handle;
				if (hwnd == IntPtr.Zero)
					throw new InvalidOperationException("The Window must be shown before extending glass.");

				// Set the background to transparent from both the WPF and Win32 perspectives
				window.Background = Brushes.Transparent;
				HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

				MARGINS margins = new MARGINS(margin);
				DwmExtendFrameIntoClientArea(hwnd, ref margins);
				return true;
			}
		}


		#endregion GlassExtension

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			dc = new InfoDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["Info"].ConnectionString);
			LoadSource();
			LoadLive();
			//LoadSystem();
			LoadDrives();

		}
		private void LoadSource()
		{
			// lbSource.DataContext 
			IQueryable<Source> sources = KC.Business.Info.GetSources(ref dc);
			
		}
		private void LoadLive()
		{
		}
		private void LoadSystem()
		{
			tSystem = new System.Timers.Timer();
			tSystem.Enabled = true;
			tSystem.Interval = 10;
			tSystem.Elapsed += new System.Timers.ElapsedEventHandler(tSystem_Elapsed);
			tSystem.Start();
		}

		private void tSystem_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			LoadDrives();
		}
		private void LoadDrives()
		{
			var drives = from d in System.IO.DriveInfo.GetDrives()
						 orderby d.Name
						 select new { d.Name, d.VolumeLabel };
			//dgDrives.ItemsSource = drives;
			//dgDrives.InputBindings.Add()
			GoFuckYourself = (CollectionView)drives;
			DataContext = (CollectionView)drives;
		}

		private void btnKillIE_Click(object sender, RoutedEventArgs e)
		{
			Process.Start(new ProcessStartInfo(@"c:\windows\system32\taskkill.exe", "/f /fi \"imagename eq iexplore.exe\""));
		}
		private void btnKillDWM_Click(object sender, RoutedEventArgs e)
		{
			Process.Start(new ProcessStartInfo(@"c:\windows\system32\taskkill.exe", "/f /fi \"imagename eq dwm.exe\""));
		}


	}
}
